<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="订单号" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户电话" prop="userPhone">
        <el-input
          v-model="queryParams.userPhone"
          placeholder="请输入用户电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="goodName">
        <el-input
          v-model="queryParams.goodName"
          placeholder="请输入用户电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="售后类型" prop="serviceType">
        <el-select
          v-model="queryParams.serviceType"
          placeholder="请选择售后类型"
          clearable
          @change="serviceTypeChange"
        >
          <el-option
            v-for="item in serviceTypeList"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain size="mini" @click="handleRefund"
          >批量退款</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="itemList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" align="center" width="50" />
      <el-table-column label="ID" align="center" width="50" prop="id" />
      <el-table-column label="订单编号" align="center" prop="orderNumber" />
      <el-table-column
        label="商品名称"
        align="center"
        width="260"
        prop="productName"
      >
        <template slot-scope="scope">
          <div class="product-box">
            <el-image
              style="width: 50px; height: 50px; margin-right: 10px"
              :src="baseURL + scope.row.productImage"
            ></el-image>
            <div>
              <div style="white-space: nowrap">{{ scope.row.productName }}</div>
              <el-tag v-if="scope.row.productSpecification">{{
                scope.row.productSpecification
              }}</el-tag>
              <el-tag v-else>无规格</el-tag>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="商品价格" align="center" width="80" prop="price">
        <template slot-scope="scope">¥{{ scope.row.price }}</template>
      </el-table-column>
      <el-table-column
        label="商品数量"
        align="center"
        width="80"
        prop="quantity"
      />

          <el-table-column
        label="用户"
        align="center"
        width="80"
        prop="userName"
      />

             <el-table-column
        label="用户id"
        align="center"
        width="80"
        prop="userId"
      />

      

      <el-table-column label="售后类型" align="center" prop="afterSalesMethod">
        <template slot-scope="scope">
          {{
            scope.row.afterSalesMethod == 0
              ? "仅退款"
              : scope.row.afterSalesMethod == 1
              ? "退款且退货"
              : ""
          }}
        </template>
      </el-table-column>

      <el-table-column label="售后状态" align="center" prop="state">
        <template slot-scope="scope">
          {{ stateList[scope.row.state - 1] }}
        </template>
      </el-table-column>

      <el-table-column
        label="售后原因"
        align="center"
        prop="afterSalesReason"
      />
      <el-table-column
        label="创建时间"
        align="center"
        width="180"
        prop="createTime"
      />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { getAfterSalesList, confirmRefund } from "@/api/system/order/manage";

export default {
  name: "Item",
  data() {
    return {
      baseURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      stateList: ["已提交", "处理中", "已完成", "用户取消", "平台取消"],
      // 订单项目表格数据
      itemList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderId: null,
        userPhone: null,
        goodName: null,
        serviceType: null,
      },
      serviceTypeList: [
        {
          value: 0,
          name: "仅退款",
        },
        {
          value: 1,
          name: "退款且退货",
        },
      ],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询售后订单项目列表 */
    getList() {
      this.loading = true;
      getAfterSalesList(this.queryParams).then((response) => {
        this.itemList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    serviceTypeChange(val) {
      console.log("val-----", val);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    // 批量退款
    handleRefund() {
      confirmRefund(this.ids).then((response) => {
        if ((response.code = 200)) {
          this.$modal.msgSuccess(response.msg);
          this.ids = [];
        }
        this.getList();
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.product-box {
  display: flex;
  justify-content: center;
}
</style>
